//按需导入Toast组件
import {Toast} from 'mint-ui'
import Vue from 'vue'
import axios from 'axios'
import config from '../config';

axios.defaults.baseURL = 'http://127.0.0.1:3000/api/json';
axios.defaults.timeout = 3000;

//在前端发给后端ajax中间进行一次拦截
axios.interceptors.request.use(config =>{
    return config;
},error =>{
    Toast({
        position: 'top',
        time: 200,
        message: "shanbi"
    });
    return Promise.reject(error);
});

//在后端返回数据给前端之前拦一下，主要做错误吗判断。
axios.interceptors.response.use(response => {
    // return response;
    switch(response.status){
       case 200:
       console.log(11111)
       console.log(response.data.data)
       console.log(utils.privateDecrypted(response.data.data))
    response.data.data = JSON.parse(utils.privateDecrypted(response.data.data) )
    console.log(response.data.data)  
             return response.data.data;
             break;
       default:
           Toast({
               position: 'top',
               time: 200,
               message: "请求失败"
       });
    break;
  }
},error => {
    //对响应错误做什么
    return Promise.reject(error);
});

export default {
    // Get 请求
   get (params,callback){
    //
       axios.defaults.headers.common['token'] = params.token;
       axios.get(params.url)
       .then(success =>{
           callback(success)
       })
       .catch(error =>{
        Toast({
            position: 'top',
            time: 200,
            message: "get 请求失败"
         });
       })
   },
  //post 请求
  post (params,callback){
    axios.post(params.url,params.bar)
    .then(response =>{
        callback(response)
    })
    .catch(error =>{
     Toast({
         position: 'top',
         time: 200,
         message: "get 请求失败"
      });
    })
}

}